package cn.huolabao.common.core.handler;

import cn.huolabao.common.core.exception.LCException;
import cn.huolabao.common.core.utils.R;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handlerMethodArgumentNotValidException(MethodArgumentNotValidException exception) {
        return R.error(exception.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public R handlerBindException(BindException bindException) {
        bindException.printStackTrace();
        return R.error(bindException.getMessage());
    }

    @ExceptionHandler(LCException.class)
    public R handlerBindException(LCException lcException) {
        return R.error(lcException.getMessage());
    }

    @ExceptionHandler({AccessDeniedException.class})
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public R handlerAccessDeniedException(AccessDeniedException e) {
        e.printStackTrace();
        return R.error("无权访问");
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R runtimeHandlerException(RuntimeException e) {
        e.printStackTrace();
        return R.error("系统错误:" + e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R exception(Exception e) {
        e.printStackTrace();
        return R.error("系统错误:" + e.getMessage());
    }

}
